Return to doc.sitecore.com

Valid for Sitecore 5.3.1
Silent Saves in Sitecore 5.3

Q:

The following method is obsolete now in Sitecore 5.3

Sitecore.Data.Items.Item.ItemEditing.EndEdit(bool updateStatistics, bool silent)

If I want to modify an item in the item:saved event how can I prevent looping now without the silent parameter?

A:

The methods Item.Editing.EndEdit(bool updateStatistics, bool silent) and EditContext(Item item, bool updateStatistics, bool silent) were removed for performance reasons. In order to prevent looping in the item:saved event you can resort to using the ThreadStatic variable which is used like a flag.

The following code is an example of how to use this variable to produce the desired effect: 

using System;
using System.Collections.Generic;
using System.Text;
using Sitecore.Data.Items;
using Sitecore.SecurityModel;
using Sitecore.Events;
using System.Collections;
namespace CustomSave
{
  public class OnSaved
  {

    [ThreadStatic]
    bool guard = false;
    internal void OnItemSaved(object sender, EventArgs args)
    {
      if (!guard)
      {
        guard = true;
        try
        {
          Item itm = Event.ExtractParameter(args, 0) as Item;
          using (SecurityDisabler disabler = new SecurityDisabler())
          {
            using (new EditContext(itm))
            {
              //If you do not wish to update the stats UpdatedBy and Updated fields then just uncomment the next line
              //itm.RuntimeSettings.ReadonlyStatistics = true;
              itm["Title"] = DateTime.Now.ToString();
            }
          }
        }
        finally
        {
          guard = false;
        }
      }
    }
  }
}